<?php

session_start();
include "config/koneksi.php";
include "config/library.php";

function formatRupiah($angka)
{
		$rupiah  = 'Rp.'.number_format($angka,0,',','.');
		return $rupiah;
}

function formatAngka($angka)
{
		$angka  = number_format($angka,0,',','.');
		return $angka;
}

function executeScalar($sql,$def=0) 
{
    $rs = mysql_query($sql) or die(mysql_error().$sql);
    if (mysql_num_rows($rs)) 
	{
		$r = mysql_fetch_row($rs);
		mysql_free_result($rs);
		return $r[0];
    }
    return $def;
}

function getTotalCardPrice()
{
	$sid 	= session_id();
	$sql	= "SELECT SUM(CARD_PRICE*QUANTITY) FROM orders_temp,card WHERE orders_temp.CARD_ID = card.CARD_ID AND ID_SESSION = '$sid' ";
	$total	= executeScalar($sql,$def=0);
	return $total;
}

function getCargoPrice($cargo_id)
{
	$sql			= "SELECT CARGO_PRICE FROM cargo WHERE CARGO_ID = '".$cargo_id."'";
	$CARGO_PRICE	= executeScalar($sql,$def=0);
	return $CARGO_PRICE;
}
	
function antiInjection($data)
{
	$filter_sql = mysql_real_escape_string(stripslashes(strip_tags(htmlspecialchars($data,ENT_QUOTES))));
	return $filter_sql;
}

function contentDouble($Content1,$Content2)
{
	return	'
				<table style="width:98.5%;padding:0px;margin:0px" collapsed="true" cellspacing="0px" cellpadding="0px" ><tr>
					<td valign="top" class="span6">'.$Content1.'</td>
					<td valign="top" class="span6">'.$Content2.'</td>
				</tr></table>
			';
}

function contentDoubleFluid($Content1,$Content2)
{
	return	'
				<table style="width:98.5%;padding:0px;margin:0px" collapsed="true" cellspacing="0px" cellpadding="0px" ><tr>
					<td valign="top" style="width:190px">'.$Content1.'</td>
					<td valign="top">'.$Content2.'</td>
				</tr></table>
			';
}

function contentSingle($Content1)
{
	return	'
				<table style="padding:2px;margin:0px" collapsed="true" cellspacing="0px" cellpadding="0px" ><tr>
					<td valign="middle">'.$Content1.'</td>
				</tr></table>
			';
}

function contentAlignRight($Content)
{
	return '
		<div style="float:right;">'.$Content.'</div>
		';
}

function getCurrentPageName() 
{
	return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

function printNumericSelect($lowerLimit,$upperLimit,$name,$selected)
{
	$string="";
	$string=$string.'<select name="'.$name.'" style="text-align:center;" class="span12">';
	for($i=$lowerLimit;$i<=$upperLimit;$i++)
	{
		$string=$string.'<option value="'.$i.'"';
		if($i==$selected)
		{
			$string=$string.'selected="selected"';
		}
		$string=$string.'>'.$i.'</option>';
	}
	$string=$string.'</select>';
	return $string;
}

function printCargoTypeSelect($name,$selected)
{
	$array=array(
		0 => "YES",
		1 => "Reguler",
	);

	$string="";
	$string=$string.'<select name="'.$name.'" style="text-align:center;" class="span12">';
	for($i=0;$i<=1;$i++)
	{
		$string=$string.'<option value="'.$i.'"';
		if($i==$selected)
		{
			$string=$string.'selected="selected"';
		}
		$string=$string.'>'.$array[$i].'</option>';
	}
	$string=$string.'</select>';
	return $string;
}

function printCargoSelect($name,$selected)
{

	$sql	= "SELECT cargo_id,city_name FROM cargo,city WHERE city.city_id=cargo.city_id";

	$array = array();
	$result = mysql_query($sql);
	if(mysql_num_rows(mysql_query($sql))>0)
	{
		while($r = mysql_fetch_array($result))
		{
			$array[$r[0]]=$r[1];
		}
	}
	
	$string="";
	$string=$string.'<select name="'.$name.'" class="span12">';
	for($i=1;$i<count($array)+1;$i++)
	{
		if(isset($array[$i]))
		{
			$string=$string.'<option value="'.$i.'"';
			if($i==$selected)
			{
				$string=$string.'selected="selected"';
			}
			$string=$string.'>'.$array[$i].'</option>';
		}
	}
	$string=$string.'</select>';
	return $string;
}

function printCitySelect()
{
	$string		= "";
	$string		= $string.'<select name="city" class="span12">
							<option value=0 selected>- Choose City -</option>';
	$tampil		= mysql_query("SELECT cargo_id,city_name FROM cargo,city WHERE city.city_id=cargo.city_id");
	while($r=mysql_fetch_array($tampil))
	{
		$string = $string.'<option value='.$r['cargo_id'].'>'.$r['city_name'].'</option>';
	}
	$string		= $string.'</select>';
	return $string;
}

function printCitySelectID($city_id)
{
	$string		= "";
	$string		= $string.'<select name="city" class="span12">
							<option value=0 selected>- Choose City -</option>';
	$tampil		= mysql_query("SELECT city_id,city_name FROM city");
	while($r=mysql_fetch_array($tampil))
	{
		$string = $string.'<option value='.$r['city_id'].' ';
		if($r['city_id']==$city_id){$string = $string.'selected="selected"';}
		$string = $string.'>'.$r['city_name'].'</option>';
	}
	$string		= $string.'</select>';
	return $string;
}

function printLegend($text)
{
	return '
		<div style="margin:0px;align:center;font-size:11pt;margin-top:10px;margin-bottom:2px;padding-top: 4px; padding-bottom: 4px;" class="legend alert alert-info">
			<b>'.$text.'</b>
		</div>';
}

function printLegendCollapsible($text,$target)
{
	return '
		<button type="button" 
			style="margin-bottom:0px;font-size:11pt" 
			class="btn btn-inverse btn-block" data-toggle="collapse" data-target="#'.$target.'">
			'.$text.'
		</button>';
}

function displayCardLineAlign($i,$lineLimit)
{
		if($i%$lineLimit==0)
		{
			return "margin-left:0px;";
		}
}

function displayCard($query,$lineLimit,$height)
{
	$result = mysql_query($query);  
	$kolom = 12/$lineLimit;
	
	$string="";
	$string=$string.'<div class="">
						<ul class="thumbnails row-fluid"  style="margin-bottom: 0px;">';
	$i=0;
	if(mysql_num_rows(mysql_query($query))>0)
	{
		while($r = mysql_fetch_array($result))
		{
			$string=$string.'
							<li class="span'.$kolom.' thumbnail"  style="min-height:'.$height.';margin-bottom: 10px;'.displayCardLineAlign($i,$lineLimit).'" >
								<a href="action.php?module=cart&act=tambah&id='.$r[0].'" class="btn btn-primary btn-small btn-block">
									<i class="icon-shopping-cart icon-white"></i>  Buy<br/>
									<span style="font-weight:bold">
									</span>
								</a>
								<!--<a href="carddisplay.php?id='.$r[0].'">-->
									<div align="center" class="" 
										style="text-align:center;font-size:9pt;padding: 3px 3px 0px;max-width:175px;margin-left:auto;margin-right:auto">
										<img src="showimage.php?id='.$r[0].'" />
										'.$r[5].'<br/><b>'.$r[4].'</b><br/><i>'.getRarity($r[0]).'</i><br/>'.formatRupiah($r[10]).'
									</div>
								<!--</a>-->
							</li>
							';
			$i=$i+1;
		}
	}
	else
	{
		$string=$string."Sorry, No Result Found";
	}
	$string=$string.'	</ul>
					</div>';
	return $string;
}

function getCardSetName($card_set_id)
{
	$sql = "SELECT card_set_name FROM card_set WHERE card_set_id='$card_set_id'";
	return executeScalar($sql,$def="");
}

function getRarity($card_id)
{
	$sql = "SELECT card_rarity_name FROM card,card_rarity WHERE card_id='$card_id' AND card_rarity.card_rarity_id = card.card_rarity_id";
	return executeScalar($sql,$def="");
}

function includeHeader()
{
	return '
					<meta name="viewport" content="width=device-width, initial-scale=1.0">
					<meta name="description" content="">
					<meta name="author" content="">

					<!-- main styles -->
						<link rel="stylesheet" href="headmaster/css/style.css" />

					<!-- Le styles -->
					<link href="assets/css/bootstrap.css" rel="stylesheet">
					<!--
					<style type="text/css">
						  body {
							padding-top: 60px;
							padding-bottom: 40px;
						  }
					</style>
					-->
					<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
					<link href="assets/css/mystyle.css" rel="stylesheet">
					 <link rel="stylesheet" href="assets/jBreadcrumbs/css/BreadCrumb.css" />
					<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
					<!--[if lt IE 9]>
						  <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
						<![endif]-->
					<!-- Fav and touch icons -->
					<link rel="shortcut icon" href="assets/ico/favicon.png">
					<!--<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
					<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
					<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
					<link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">-->
					<!-- colorbox -->
					<script src="assets/js/jquery.js"></script>
					<script src="jquery.colorbox.js"></script>
					<link rel="stylesheet" href="colorbox.css" />
					<link rel="stylesheet" href="treeview.css" />
					<script>
						$(document).ready(function () {
							//$("#cari").hide();
							$("#jCrumbs").jBreadCrumb({
									endElementsToLeaveOpen: 0,
									beginingElementsToLeaveOpen: 0,
									timeExpansionAnimation: 500,
									timeCompressionAnimation: 500,
									timeInitialCollapse: 500,
									previewWidth: 30
								});
								//colorbox
								$(".show").colorbox({width:600,height:400});
						});
					</script>
					
			
		<script>
			//* hide all elements & show preloader
			document.documentElement.className += \'js\';
			$("#cardTabel").dataTable();
		</script>
		';
}

function printModalLogin()
{
	return '
		<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
			<h3 id="myModalLabel">Login</h3>
		</div>
		<div class="modal-body">
			<form class="form-horizontal" action="checkLogin.php" method="post">
				<div class="control-group">
					<label class="control-label" for="inputEmail">Username</label>
					<div class="controls">
						<input type="text" name="user" id="inputnickname" placeholder="Username">
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputPassword">Password</label>
					<div class="controls">
						<input type="password" name="pass" id="inputPassword" placeholder="Password">
						<input type="hidden" name="controlSender" id="controlSender" value="login">
						<input type="hidden" name="pageSender" id="pageSender" value="">
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="submit" class="btn btn-inverse">Proceed <i class="icon-chevron-right icon-white"></i></button>
					</div>
				</div>
			</form>
		</div>
		</div>
	';
}

function printTransactionDetail($transaction_id)
{
	$sqlstring="SELECT * FROM buy_transaction_detail, Card, Card_Rarity WHERE
						buy_transaction_detail.buy_transaction_id='$transaction_id' AND
						buy_transaction_detail.CARD_ID = card.CARD_ID AND
						Card.Card_Rarity_ID=Card_Rarity.Card_Rarity_ID";
	//echo $sqlstring;
	$sql = mysql_query($sqlstring);

	$string= '
				<table class="table table-striped table-hover table-condensed" style="margin-bottom: 5px;font-size:9pt">
				<thead class="">
					<th>No</th>
					<th>Image</th>
					<th>Card</th>
					<th style="text-align:center">Buying Qty</th>
					<th style="text-align:center">Price</th>
					<th style="text-align:center">Sub Total</th>
				</thead>  
				<tbody>';
	$total	= 0;
	$no 	= '1';
	while($r= mysql_fetch_array($sql))
	{
		$subtotal    = $r['CARD_PRICE']*$r['CARD_BUY_QUANTITY'];
		$total       = $total+$subtotal; 
		$harga       = formatAngka($r['CARD_PRICE']);
		$name		 = 'jml['.$no.']';
		
		$string=$string.'
					<tr>
						<td>'.$no.'</td>
						<td><img src="showimage.php?id='.$r['CARD_ID'].'" width="36px"/></td>
						<td>'.$r['CARD_NAME'].'<br/><b>'.$r['CARD_CODE'].'</b> - <i>'.$r['CARD_RARITY_NAME'].'</i></td>
						<td style="text-align:right">'.$r['CARD_BUY_QUANTITY'].'</td>
						<td style="text-align:right">'.$harga.'</div></td>
						<td style="text-align:right">'.formatAngka($subtotal).'</div></td>
					</tr>';
		$no++; 
	}
	
	$cargo_id = executeScalar("SELECT cargo_id from buy_transaction where buy_transaction_id='$transaction_id'",$def=0);
	$cargo_price=getCargoPrice($cargo_id);
	$string=$string.'
					<tr class="warning">
						<td colspan=5>'.contentAlignRight('Total Card(s) Price').'</td>
						<td colspan=1>'.contentAlignRight(''.formatRupiah($total).'').'</td>
						<td></td>
					</tr>
					<tr class="warning">
						<td colspan=5>'.contentAlignRight('Delivery Price').'</td>
						<td colspan=1>'.contentAlignRight(''.formatRupiah($cargo_price).'').'</td>
						<td></td>
					</tr>
					<tr class="warning">
						<td colspan=5>'.contentAlignRight('<b>Final Payment</b>').'</td>
						<td colspan=1>'.contentAlignRight('<b>'.formatRupiah($cargo_price+$total).'</b>').'</td>
						<td></td>
					</tr>
				</tbody>
			</table>
		';
	return $string;
}

/*----------------------------------------------------------------------------------------------------
 AREA MENU START
----------------------------------------------------------------------------------------------------*/

function printLoginStat()
{
	$nick_duelist="Guest";
	if(isset($_SESSION['levelUser']))
	{
		if($_SESSION['levelUser']=="duelist")
		{
			$string='<li style="float:right">
						<a href="logout.php" style="padding: 0px;color:#ffffff">
							<button type="button" class="btn btn-primary btn-small"><i class="icon-off icon-white"></i> Logout</button>
						</a>
					</li>';
			$string=$string.'<li style="float:right" '.setActiveMenu('profile.php').'><a href="profile.php"><i class="icon-user"></i> <i>'.$_SESSION['duelist_nick'].'</i></a></li>';
		}
	}
	else
	{
		$string='<li style="float:right">
					<a href="#myModal" style="padding:0px;" role="button" data-toggle="modal"
					onclick="$(\'#controlSender\').val(\'login\');$(\'#pageSender\').val(\''.getCurrentPageName().'\');"
					>
					<button type="button" class="btn btn-small btn-primary"><i class="icon-off icon-white"></i> Login</button></a>
				</li>
				<li style="float:right">
					<a href="register.php" style="padding:0px;">
						<button type="button" class="btn btn-small btn-primary"><i class="icon-plus icon-white"></i> Register</button></a>
					</a>
				</li>';
	}
	
	return $string;
}

function setActiveMenu($pageName)
{
	if(getCurrentPageName()==$pageName)
	{
		return ' class="active" ';
	}
	else
	{
		return '';
	}
}

function printMenu()
{
	return '
	<div class="row-fluid" >
		<div class="navbar navbar-fixed-top">
			<div class="navbar-inner">
				<div class="container">
					<ul class="nav nav-pills span12">
						<li'.setActiveMenu('index.php').	'><a href="index.php"><i class="icon-home "></i> Home</a></li>
						<li'.setActiveMenu('product.php').	'><a href="product.php"><i class="icon-tags "></i> Products</a></li>
						<li'.setActiveMenu('cart.php').		'><a href="cart.php"><i class="icon-shopping-cart "></i> Shopping Cart ('.getCartCount().')</a></li>
						<li'.setActiveMenu('faq_transaction.php').		'><a href="faq_transaction.php"><i class="icon-question-sign "></i> FAQ</a></li>
						<form class="navbar-search" action="search.php" method="get">
							<div class="input-append">
								<input class="" id="nama" name="nama" type="text" placeholder="Search Cards by Name" style="width: 150px;">
								<span class="add-on"><i class="icon-search"></i></span>
							</div>
						</form>
						'.printLoginStat().'
						</ul>
					</ul>
				</div>
			</div>
		</div>
	</div>
	';
}

/*----------------------------------------------------------------------------------------------------
 AREA MENU END
----------------------------------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------------------------------
 AREA SIDEBAR START
----------------------------------------------------------------------------------------------------*/
function printLogo()
{
	return '
		<div class="row-fluid">
			<div class="span12">
				<center style="margin-bottom: 10px;">
					<a href="index.php">
						<img src="./assets/ico/logopanjangTCG-REV05-210.png"/>
					</a>
				</center>
			</div>
		</div>
		';
}

function getCartCount()
{
	$sid	= session_id();
	$querytext = "SELECT SUM(quantity) AS totaljumlah FROM orders_temp WHERE id_session = '".$sid."'";
	$query	= mysql_query($querytext);
	if (executeScalar($querytext) != 0)
	{
		$total = executeScalar($querytext);
		return $total;
	}
	else
	{
		return '0';
	}
}

function getCartPrice()
{
	$sid	= session_id();
	$querytext = "SELECT SUM(card_price*quantity) FROM orders_temp, Card WHERE id_session='$sid' AND orders_temp.CARD_ID = card.CARD_ID";
	$query	= mysql_query($querytext);
	if (executeScalar($querytext) != 0)
	{
		$total = executeScalar($querytext);
		return formatRupiah($total);
	}
	else
	{
		return formatRupiah('0');
	}
}

function printCartButton()
{
	return '
				<a href="cart.php">
					<div class="alert alert-info btn-block" style="margin-bottom: 0px; text-align: center; padding: 4px;">
						<img src="./assets/ico/shopping_trolley-48.png"/>
						<b>'.getCartCount().' Item /
						'.getCartPrice().'</b>
					</div>
				</a>
		';
}

function printCardSetTree()
{
	$sql	= "SELECT card_set_type_id,card_set_type_name FROM card_set_type";

	$array_card_set_type = array();
	$result = mysql_query($sql);
	if(mysql_num_rows(mysql_query($sql))>0)
	{
		while($r = mysql_fetch_array($result))
		{
			$array_card_set_type[$r[0]]=$r[1];
		}
	}
	
	$string="";
	for($i=1;$i<count($array_card_set_type)+1;$i++)
	{
		$string=$string.'
					<div class="css-treeview">
						<ul class="nav nav-stacked nav-tabs">
							<li style="padding-top:4px"><input type="checkbox" id="item-'.$i.'" /><label for="item-'.$i.'">'.$array_card_set_type[$i].'</label>
								<ul>';
		$sqlin		= 'SELECT card_set_id,card_set_name FROM card_set WHERE card_set_status="PUBLISHED" AND card_set_type_id='.$i;
		$resultin	= mysql_query($sqlin);
		if(mysql_num_rows(mysql_query($sqlin))>0)
		{
			while($r = mysql_fetch_array($resultin))
			{
				$string=$string.'	<li>  <i class="icon-ok-sign"> </i> <a href="search.php?set_id='.$r[0].'">'.$r[1].'</a></li>';
			}
		}
		$string=$string.'
								</ul>
							</li>
						</ul>
					</div>';
	}
	return $string;
}

function printBankAccount()
{
	return '
			<ul class="nav nav-tabs nav-stacked">
				<li><a style="padding: 4px;color:#000000;font-size:9pt"><b>BCA</b> - 2291213300<br/>a/n Muliadi Wijaya drs</a></li>
				<li><a style="padding: 4px;color:#000000;font-size:9pt"><b>Mandiri</b> - 1180006628001<br/>a/n drs. Muliadi Wijaya</a></li>
			</ul>
			';
}

function printContact()
{
	return '
			<ul class="nav nav-stacked nav-tabs"  style="margin-bottom: 10px;">
				<li style="margin-top: -5px;">
					<a href="https://facebook.com/NusaKreatif" style="padding: 4px;font-size:9pt; padding-bottom: 2px;"><img src="assets/img/facebook-icon-24.png"/> Nusa Kreatif
					</a>
				</li>
				<li >
					<a href="https://twitter.com/nusakreatif" style="padding: 4px;font-size:9pt"><img src="assets/img/twitter-icon-24.png"/> @nusakreatif
					</a>
				</li>
			</ul>
			';
}

function printSidebar()
{
	$string="";
	$string=$string.
			printLogo().
			printCartButton().printLegend('Card Sets').printCardSetTree();
			
	if(isset($_SESSION['levelUser']))
	{
		if($_SESSION['levelUser']=="duelist")
		{
			$string=$string.printLegend('Bank Account').printBankAccount();
		}
	}
			
	$string=$string.printLegend('Contact Us').printContact();
			
	return $string;
}
/*----------------------------------------------------------------------------------------------------
 AREA SIDEBAR END
----------------------------------------------------------------------------------------------------*/

function includeFooter()
{
	return '
			<!-- Le javascript
				================================================== -->
			<!-- Placed at the end of the document so the pages load faster -->
			<script src="assets/js/bootstrap-transition.js"></script>
			<script src="assets/jBreadcrumbs/js/jquery.jBreadCrumb.1.1.min.js"></script>
			<script src="assets/js/bootstrap-alert.js"></script>
			<script src="assets/js/bootstrap-modal.js"></script>
			<script src="assets/js/bootstrap-dropdown.js"></script>
			<script src="assets/js/bootstrap-scrollspy.js"></script>
			<script src="assets/js/bootstrap-tab.js"></script>
			<script src="assets/js/bootstrap-tooltip.js"></script>
			<script src="assets/js/bootstrap-popover.js"></script>
			<script src="assets/js/bootstrap-button.js"></script>
			<script src="assets/js/bootstrap-collapse.js"></script>
			<script src="assets/js/bootstrap-carousel.js"></script>
			<script src="assets/js/bootstrap-typeahead.js"></script>
			
			<script src="headmaster/js/jquery.min.js"></script>
			<!-- smart resize event -->
			<script src="headmaster/js/jquery.debouncedresize.min.js"></script>
			<!-- hidden elements width/height -->
			<script src="headmaster/js/jquery.actual.min.js"></script>
			<!-- js cookie plugin -->
			<script src="headmaster/js/jquery.cookie.min.js"></script>
			<!-- main bootstrap js -->
			<script src="headmaster/bootstrap/js/bootstrap.min.js"></script>
			<!-- bootstrap plugins -->
			<script src="headmaster/js/bootstrap.plugins.min.js"></script>
			<!-- tooltips -->
			<script src="headmaster/lib/qtip2/jquery.qtip.min.js"></script>
			<!-- jBreadcrumbs -->
			<script src="headmaster/lib/jBreadcrumbs/js/jquery.jBreadCrumb.1.1.min.js"></script>
			<!-- sticky messages -->
            <script src="headmaster/lib/sticky/sticky.min.js"></script>
			<!-- fix for ios orientation change -->
			<script src="headmaster/js/ios-orientationchange-fix.js"></script>
			<!-- scrollbar -->
			<script src="headmaster/lib/antiscroll/antiscroll.js"></script>
			<script src="headmaster/lib/antiscroll/jquery-mousewheel.js"></script>
            <!-- common functions -->
			<script src="headmaster/js/gebo_common.js"></script>
    
			<!-- colorbox -->
			<script src="headmaster/lib/colorbox/jquery.colorbox.min.js"></script><!-- datatable -->
			<script src="headmaster/lib/datatables/jquery.dataTables.min.js"></script>
			<!-- additional sorting for datatables -->
			<script src="headmaster/lib/datatables/jquery.dataTables.sorting.js"></script>
			<!-- tables functions -->
			<script src="headmaster/js/gebo_tables.js"></script>

			<script>
				$(document).ready(function() {
					//* show all elements & remove preloader
					setTimeout(\'$("html").removeClass("js")\',1000);
				});
			</script>
		';
}

function printPage($Title,$Content)
{
	return '
			<!DOCTYPE html>
			<html lang="en">
				<head>
					<meta charset="utf-8">
					<title>'.$Title.'</title>
					'.includeHeader().'
				</head>
				<body style="padding-bottom: 20px;">
					<div class="container">'.printMenu().'</div>'.printModalLogin().'
					<div class="container thumbnail">
						<div class="row-fluid">
							<div class="span12" style="height:15px">
							</div>
							<div class="span3" style="margin-bottom:10px; margin-left:0px">
								'.printSideBar().'
							</div>
							<div class="span9">
								'.$Content.'
							</div>
						</div>
						<div class="btn disabled btn-block" style="margin-bottom: 0px;">
							<a href="http://nusakreatif.com" style="color:#000000">
								Powered by NusaKreatif &copy; 2012
							</a>
						</div>
					</div>
					'.includeFooter().'
				</body>
			</html>
		';

}

function validatePage()
{
	if(isset($_SESSION['levelUser']))
	{
		if($_SESSION['levelUser']=="duelist")
		{
			if (
				!(getCurrentPageName()=="index.php" || 
				getCurrentPageName()=="product.php" || 
				getCurrentPageName()=="faq_transaction.php" || 
				getCurrentPageName()=="about.php" || 
				getCurrentPageName()=="search.php" || 
				getCurrentPageName()=="cart.php" ||
				getCurrentPageName()=="confirmAddress.php" ||
				getCurrentPageName()=="finishingOrder.php" ||
				getCurrentPageName()=="savingOrder.php" ||
				getCurrentPageName()=="about.php" || 
				getCurrentPageName()=="profile.php" ||
				getCurrentPageName()=="updateProfile.php" ||
				getCurrentPageName()=="viewTransactionDetail.php" ||
				getCurrentPageName()=="confirmPayment.php" ||
				getCurrentPageName()=="updateTransaction.php" ||
				getCurrentPageName()=="purgeIdleTransaction.php" ||
				getCurrentPageName()=="logout.php")
			)
			{header("Location: ./index.php");exit();}
		}
	}
	else
	{
		if (
			!(getCurrentPageName()=="index.php" || 
			getCurrentPageName()=="product.php" || 
			getCurrentPageName()=="faq_transaction.php" || 
			getCurrentPageName()=="about.php" || 
			getCurrentPageName()=="search.php" || 
			getCurrentPageName()=="cart.php" ||
			getCurrentPageName()=="mail.php" ||
			getCurrentPageName()=="login.php" || 
			getCurrentPageName()=="register.php" || 
			getCurrentPageName()=="savingRegister.php" || 
			getCurrentPageName()=="purgeIdleTransaction.php" ||
			getCurrentPageName()=="checkLogin.php")
		)
		{header("Location: ./index.php");exit();}
	}
}

function purgeIdleTransaction()
{
	$sql = "SELECT bt.BUY_TRANSACTION_ID,
				 BUY_TRANSACTION_ORDER_DATE,
				 DATE_ADD(`BUY_TRANSACTION_ORDER_DATE`,INTERVAL 2 DAY),
				 NOW(),
				 btd.card_id,
				 btd.card_buy_quantity,
				 c.CARD_BOOKED_QUANTITY
			FROM `buy_transaction` bt
			JOIN `buy_transaction_detail` btd ON bt.BUY_TRANSACTION_ID = btd.BUY_TRANSACTION_ID
			JOIN card c ON btd.card_id = c.card_id
			WHERE  `BUY_TRANSACTION_STATUS` =  'unpaid'
			AND NOW() >= DATE_ADD(`BUY_TRANSACTION_ORDER_DATE`, INTERVAL 2 DAY)";
			
	$hasil = mysql_query($sql);
	
	while($exec=mysql_fetch_array($hasil))
	{
		$card_id=$exec[4];
		$card_buy_quantity=$exec[5];
		$CARD_BOOKED_QUANTITY=$exec[6];
		$CARD_BOOKED_QUANTITY=$CARD_BOOKED_QUANTITY-$card_buy_quantity;
		mysql_query("UPDATE card SET card_booked_quantity='$CARD_BOOKED_QUANTITY' WHERE card_id='$card_id'");
	}

	$sql = "SELECT buy_transaction_id
			FROM `buy_transaction` 
			WHERE  `BUY_TRANSACTION_STATUS` =  'unpaid'
			AND NOW() >= DATE_ADD(`BUY_TRANSACTION_ORDER_DATE`, INTERVAL 2 DAY)";
		
	$hasil = mysql_query($sql);
	
	while($exec=mysql_fetch_array($hasil))
	{
		$string="UPDATE buy_transaction SET BUY_TRANSACTION_STATUS='cancelled' WHERE buy_transaction_id='$exec[0]'";
		mysql_query($string);
	}
}

validatePage();

?>
